package com.googlecode.dtools.typeinfo.work.impl;

import com.googlecode.dtools.typeinfo.beans.DmType;
import com.googlecode.dtools.typeinfo.beans.ObjectFactory;
import com.googlecode.dtools.typeinfo.work.FilesWorker;

import javax.xml.bind.JAXBElement;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: A_Reshetnikov
 * Date: 10.06.11
 * Time: 16:04
 * To change this template use File | Settings | File Templates.
 */
public class FilesWorkerImpl implements FilesWorker {


    public void setFileWithTypeNames(String fileWithTypeNames) {
        this.fileWithTypeNames = fileWithTypeNames;
    }

    public void setOutputPath(String outputPath) {
        this.outputPath = outputPath;
    }

    private String fileWithTypeNames;

    public String getOutputPath() {
        return outputPath;
    }

    private String outputPath;

    public ArrayList<String> readTypeNameList() {

        ArrayList<String> list = new ArrayList<String>();
        try {
            // Open the file that is the first
            // command line parameter
            FileInputStream fStream = new FileInputStream(this.fileWithTypeNames);
            // Get the object of DataInputStream
            DataInputStream in = new DataInputStream(fStream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
            //Read File Line By Line
            while ((strLine = br.readLine()) != null) {
                // Print the content on the console
                System.out.println(strLine);
                if (!strLine.startsWith("#") && !strLine.trim().equals(""))
                    list.add(strLine);
            }
            //Close the input stream
            in.close();
        } catch (Exception e) {//Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }

        return list;
    }

    public void saveXml(DmType dmType) {
        JAXBElement element = new ObjectFactory().createDmType(dmType);
        try {
            // create a JAXBContext capable of handling classes generated into package
            javax.xml.bind.JAXBContext jaxbContext
                    = javax.xml.bind.JAXBContext.newInstance("com.googlecode.dtools.typeinfo.beans");

            // create a Marshaller and do marshal
            javax.xml.bind.Marshaller marshaller = jaxbContext.createMarshaller();

            marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            // marshaller.setProperty(Marshaller.JAXB_ENCODING, "windows-1251");
           // marshaller.marshal(element, System.out);
            marshaller.marshal(element, new FileOutputStream(outputPath + dmType.getName() + ".xml"));
        } catch (javax.xml.bind.JAXBException je) {
            je.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public void serialize(List<DmType> dmTypeList) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public List<DmType> deserialize() {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public void write4Wiki(String fileName, String fileText) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public void writeDDL(String fileText) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

}
